<?php	
/*\
 *	oh.config.interface.php
 *	-----------------------
 *	name: config
 *	desc: this module provide manage a configuration system
 *	hash: 
 *	date: 24-09-2010
\*/	oh_module_header();

# main function
function oh_function_config($m) {	
	if (count($m->token) > 0) {		
		global $oh_object_config;
		$output = $oh_object_config->config;
		foreach($m->token as $token) {
			if (!isset($output[$token])) {
				//oh_message("configuration value not defined: ".$token);
				return "";
			}
			$output = $output[$token];
		}
		return $output;
	} else {
		return oh_this();
	}		
} 
	
#module main class
class oh_class_config {
	public $config;
	
	function from_string($string) {
	    $o = array();
	    $string = str_replace(".","__DOT__",$string);
	    parse_str($string,$array);		    
	    foreach($array as $key=>$value) {
		    $value = str_replace("__DOT__",".",$value);
	        $keyc = explode("__DOT__",$key);
	        if (count($keyc)==1) $o[$keyc[0]] = $value;
	        if (count($keyc)==2) $o[$keyc[0]][$keyc[1]] = $value;
	    }
	    $this->from_array($o);		    
	}
	function from_array($array) {
		$this->iteration("",$array);
	}
	function from_file($filename) {
		define("__CONFIG__",$filename);
		$file = pathinfo($filename);
		switch ($file['extension']) {
			case 'php': 
				//require_once($filename);
				$php = file_get_contents($filename);
				$php = str_replace("<?php","",$php);
				$php = str_replace("?>","",$php);
				$php = str_replace("<?","",$php);
				$s = strpos($php,'$');
				$e = strpos($php,'=');
				$php = substr($php,0,$s).'$oh_config '.substr($php,$e);
				eval($php);
				$this->iteration("",$oh_config);
				break;
		}			
	}
	function from_wordpress($filename) {
	}
	function from_joomla($filename) {
	}
	function file($filename) {
		$this->from_file($filename);
	}
	function set($key, $value) {
		$tokens = explode(".", $key);
		if (count($tokens)> 5) {oh_message("Error, more token in config value.","set");}
		if (count($tokens)==1) {$this->config	[$tokens[0]]=$value;return;}
		if (count($tokens)==2) {$this->config	[$tokens[0]]
												[$tokens[1]]=$value;return;}
		if (count($tokens)==3) {$this->config	[$tokens[0]]
												[$tokens[1]]
												[$tokens[2]]=$value;return;}
		if (count($tokens)==4) {$this->config	[$tokens[0]]
												[$tokens[1]]
												[$tokens[2]]
												[$tokens[3]]=$value;return;}
		if (count($tokens)==5) {$this->config	[$tokens[0]]
												[$tokens[1]]
												[$tokens[2]]
												[$tokens[3]]
												[$tokens[4]]=$value;return;}
		if (count($tokens)==6) {$this->config	[$tokens[0]]
												[$tokens[1]]
												[$tokens[2]]
												[$tokens[3]]
												[$tokens[4]]
												[$tokens[5]]=$value;return;}
	}
	function tree() {
		return $this->config;
	}
	function iteration($base="",$array) {
		foreach($array as $key=>$value) {
			if (is_array($value)) {
				$this->iteration(oh_name_sanitize($key).".",$value);
			} else {					
				//echo $base.oh_name_sanitize($key).":".$value."<br>";
				$this->set($base.oh_name_sanitize($key),$value);
			}
		}
	}
	function have($key) {
		$tokens = explode(".", $key);
		if (count($tokens) == 2) {
			if (isset($this->config[$tokens[0]][$tokens[1]])) {
				if ($this->config[$tokens[0]][$tokens[1]] != "") {
					return true;
				} else {
					return false;	
				}
			} else {
				return false;
			}
		}
	}
}
